package Year21_Month12.day1204;

public class Solution {
        public int trap(int[] height) {
            int left = 0,right = height.length - 1;
            int water = 0;
            int leftmax = 0,rightmax = 0;
            while(left < right){
                leftmax = Math.max(leftmax,height[left]);
                rightmax = Math.max(rightmax,height[right]);
                if(height[left] > height[right] ){
                    water += rightmax - height[right];
                    --right;
                }else{

                    water += leftmax - height[left];
                    ++left;
                }
            }
            return water;
    }
}
/*


     public int trap(int[] height) {
        int maxheight = 0;
        for(int i = 0;i < height.length;i++){
            if(height[maxheight] > height[i]){
                maxheight = i;
            }
        }
        int left = 0;
        int right = height.length  - 1;
        int water = 0;
        int facleft = 0;//用来记录左边水位线
        int facright = 0;//用来记录右边水位线
        while(left < maxheight || right > maxheight){
            if(left < maxheight){
                if(height[left] > facleft){
                    facleft = height[left];
                }else{
                    water += facleft - height[left];
                }
            }

            if(right > maxheight){
                if(height[right] > facright){
                    facright = height[right];
                }else{
                    water += facright - height[right];
                }
            }
        }
        return water;
    }
 */